<?php
namespace app\home\controller;
use app\home\model\LoginModel;
use \think\Controller;
use \think\Db;
use \think\Session;
use think\Loader;
use think\Request;
use think\Cache;
class login extends Controller{
    /*
     * 登录页面
     * */
    public function login(){
        return $this->fetch("login");
    }
    /*
     *注册页面
     * */
    public function reg(){
        return $this->fetch("reg");
    }
    /*
     * 注销
     * */
    public function cancellAtion(){
        session(null);
        $this->redirect('home/Index/index');
    }
    /*
     * 找回密码页面
     */
    public function back(){
        return $this->fetch("back_password");
    }
    /*
     * 绑定手机页面
     * */
    public function binding(){
//            var_dump(Session::get('rand'));
//            exit;
        return $this->fetch("binding_phone");
    }
    /*
     * 验证码页面
     * */
    public function code(){
//        var_dump(Session::get('rand'));
//        exit;
        return $this->fetch("Confirmation_code");
    }
    /*
 * 找回密码验证码页面
 * */
    public function codePhone(){
//        var_dump(Session::get('rand'));
//        exit;
        return $this->fetch("Confirmation_phone");
    }

/*
 * 登录
 * */
    public function userLogin()
    {

        $userName = input("userName");
        $data=[
            'user_name'=>$userName
        ];
        $pwd = md5(input("pwd"));
        $model=new LoginModel();
        $rows=$model->query($data);
        //判断数据是否为空
        if($userName=="" || $pwd==""){
            echo 1;
        }//对比密码和验证是否有该账号存在
        else if($rows==NULL||$rows["user_pwd"]!==$pwd){
            echo  2;
        }else if($rows["user_pwd"]==$pwd){
            Session::set('USER',$rows);
            echo 3;
        }
}
/*
 * 注册
 * */
    public function regLogin()
    {
        $userName = input("userName");
        $data=[
            'user_name'=>$userName
        ];
        $pwd = input("pwd");
        $pwdTwo = input("pwds");
        $nickName = input("nickName");
        $phone = input("phone");
        $model = new LoginModel();
        $row=$model->query($data);
        $phoneInfo=$model->Phone($phone);
        //判断是否有空的数据
         if (empty($userName) || empty($pwd) || empty($pwdTwo) || empty($nickName)) {
            echo 2;
        }//判断密码和确认密码是否一致
        else if ($pwd !== $pwdTwo) {
             echo 4;
         }//判断密码长度是否符合规定
         else if (strlen($pwd)>16 || strlen($pwd)<6){
             echo 3;
         }//判断账号是否存在
         else if ($row!==NULL){
             echo 6;
         }//成功注册
         else{
             $insert = [
                'user_name' => $userName,
                'user_pwd' => md5($pwd),
                'is_del' => 0,
                'reg_time' => time(),
                'nickname' => $nickName,
                'sex' => 0
        ];
        $rows = $model->reg($insert);
        $res = $model->query($data);
        Session::set('USER',$res);
        ECHO 1;
    }
    }
    public function backLogin(){
        $phone = input("phone");
        $model=new LoginModel();
        $rows=$model->Phone($phone);
    }
    /**
     * 微信授权登录
     */
    public function getCode(){
        $config=['redirect_uri'=>'http://119.23.239.149/home/Login/setInfo'];
        $weixinAuth=new Weixinauth($config);
        $weixinAuth->code();
    }
    public function setInfo(){

        $weixinAuth=new Weixinauth();
        if(!$weixinAuth->getAuthAccessToken()){
            $this->error('授权失败',url('home/Login/login'));
        }
        $userInfo = $weixinAuth->getUserInfo();
        if(!$userInfo){
            $this->error('授权失败',url('home/Login/login'));
        }
        $insert = [
            'open_id'=>$userInfo['openid'],
            'nickname'=>$userInfo['nickname'],
            'sex'=>$userInfo['sex'],
            'avatar_url'=>$userInfo['headimgurl']
        ];
        $openId=$userInfo['openid'];
        $model= new LoginModel();
        $res = $model->weixinLogin($openId);
        if($res==NULL){
            $rows = $model->reg($insert);
            Session::set('USER',$res);
            $this->redirect('home/Login/binding');
        }else{
            Session::get('USER');
            $this->redirect('home/Index/index');
        }
    }
    public function verificAtionCode(){
        $code=md5(input('code'));
        $random=Session::get('rand');
        if($code==$random){
           $data=[
               'id'=>Session::get('USER.id'),
             'phone'=>Session::get('phone')
           ];
            $model=new LoginModel();
            $res=$model->modify($data);
            if($res==true){
                echo 1;
            }else{
                echo 2;
            }
        }else{
            echo 3;
        }
    }

    public function verificAtionCodephone(){
        $phone=Session::get('phone');
        $code=md5(input('code'));
        $random=Session::get('rand');
        if($code==$random) {
            $model=new LoginModel();
            $res=$model->phone($phone);
            Session::set('USER',$res);
            echo 1;
        }else{
            echo 2;
        }
    }

}